package com.imphead.demo1.controller;

import com.imphead.demo1.config.CustomException;
import com.imphead.demo1.config.SystemMain;
import com.imphead.demo1.model.User_info;
import com.imphead.demo1.service.User_infoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Api(tags = "用户信息管理")
@RestController
@RequestMapping("/user")
public class User_infoController {

    @Autowired(required = false)
    public User_infoService userService;

    //private User_infoService userService=BeanUtils.getBean(User_infoService.class);

    @ApiOperation("测试接口")
    @GetMapping("/hello")
    public String hello() {
        //SystemMain.log.info("hello");
        log.info("hello");
        return "hello";
    }

    @ApiOperation("增加用户")
    @PostMapping("/addUser")
    public int addUser( @RequestBody User_info user){
        //SystemMain.log.info("addUser");
        log.info("addUser");
        user.setCreate_time(new Date());
        user.setId(UUID.randomUUID().toString());
        return userService.addUser(user);
    }

    @ApiOperation("删除用户")
    @PostMapping("/delUser")
    public int delUser( @RequestBody String  id)
    {
        //SystemMain.log.info("delUser");
        log.info("delUser");
        return userService.delUser(id);
    }

    @ApiOperation("修改用户")
    @PostMapping("/editUser")
    public int editUser( @RequestBody User_info  user){
        //SystemMain.log.info("editUser");
        log.info("editUser");
        user.setCreate_time(new Date());
        return userService.editUser(user);
    }

    @ApiOperation("查询列表")
    @GetMapping("/selectList")
    public List<User_info> selectList()
    {
        //SystemMain.log.info("selectList");
        log.info("selectList");
        return userService.selectList();
    }

    @ApiOperation("测试报错")
    @GetMapping("/something")
    public ResponseEntity<String> doSomething()
    {
        SystemMain.log.trace("This is a Main trace message.");
        SystemMain.log.debug("This is a Main debug message.");
        SystemMain.log.info("This is a Main info message.");
        SystemMain.log.warn("This is a Main warn message.");
        SystemMain.log.error("This is a Main error message.");

        log.trace("This is a Main trace message.");
        log.debug("This is a Main debug message.");
        log.info("This is a Main info message.");
        log.warn("This is a Main warn message.");
        log.error("This is a Main error message.");

        // 假设某个条件不满足，抛出自定义异常
        throw new CustomException("INVALID_INPUT", "Invalid input provided");
    }
}
